
/** 
 * AUTHOR: 雾雨魔理沙
 * TIME: 2024.03.03-15.07.10
 * ATTENTION: onStart 等UI脚本自带函数不可改写为异步执行，有需求的异步逻辑请使用函数封装，通过函数接口在内部使用
 */

// import { cubicBezier } from "../Common/utils";
import { cubicBezier } from "../Common/utils";
import MatchEndControlsUI_Generate from "../ui-generate/MatchEndControlsUI_generate";
import MatchEndConfirmUI from "./MatchEndConfirmUI";


export default class MatchEndControlsUI extends MatchEndControlsUI_Generate {

	/**菜单显示状态 */
	private isShow = false;
	/**当前正在执行的tween */
	private curTween: Tween<any> = null;
	/**动画时间 */
	private animTime: number = 500;


	/** 
	 * 构造UI文件成功后，在合适的时机最先初始化一次 
	 */
	protected onStart() {
		// //设置能否每帧触发onUpdate
		// this.canUpdate = false;
		// this.layer = UILayerMiddle;

		// // 隐藏除展开按钮以外的所有控件
		// const hideButtons = () => {
		// 	const isVisible = this.tieButton.visibility == SlateVisibility.Hidden;
		// 	[this.bgImage, this.tieButton, this.giveUpButton, this.runAwayButton, this.recoverButton].forEach(button => {
		// 		button.visibility = isVisible ? SlateVisibility.Visible : SlateVisibility.Hidden;
		// 	});
		// };

		// // 初始状态下隐藏除展开按钮以外的所有控件
		// hideButtons();

		// // 点击展开按钮时切换其他按钮的可见性
		// this.expandButton.onClicked.add(hideButtons);




		this.initBtn();
	}


	protected onShow(...params: any[]) {

	}

	/**初始化按钮点击事件 */
	private initBtn() {
		this.expandButton.onClicked.add(() => {
			if (this.curTween) return console.warn("当前正在播动画！！");
			if (!this.isShow) {
				this.showMenuAnim();
			} else {
				this.hideMenuAnim();
			}
		});
		// 平局、逃跑、认输按钮点击事件
		[this.tieButton, this.runAwayButton, this.giveUpButton].forEach(button => {
			button.onClicked.add(() => handleClick(button));
		});

		// recoverButton 点击事件
		this.recoverButton.onClicked.add(()=>{
			if (this.curTween) return console.warn("当前正在播动画！！");
			this.hideMenuAnim()});
	}

	/**展开菜单canvas */
	public showMenuAnim() {
		let startPos = this.bgImage.position.clone();
		let endPos = new Vector2(this.bgImage.position.x, this.bgImage.position.y + this.bgImage.size.y);
		this.curTween = new mw.Tween({ pos: startPos.clone() }).to({ pos: endPos }, this.animTime)
			.onUpdate((obj) => {
				this.bgImage.position = obj.pos;
			})
			.onComplete(() => {
				this.curTween = null;
				this.isShow = true;
			})
			.easing(cubicBezier(0, .56, .65, 1))
			.start();

			// 显示全屏点击收起按钮
			this.recoverButton.visibility=SlateVisibility.Visible
	}


	/**隐藏菜单canvas */
	public hideMenuAnim() {
		let startPos = this.bgImage.position.clone();
		let endPos = new Vector2(this.bgImage.position.x, this.bgImage.position.y - this.bgImage.size.y);
		this.curTween = new mw.Tween({ pos: startPos.clone() }).to({ pos: endPos }, this.animTime)
			.onUpdate((obj) => {
				this.bgImage.position = obj.pos;
			})
			.onComplete(() => {
				this.curTween = null;
				this.isShow = false;
			})
			.easing(cubicBezier(0, .56, .65, 1))
			.start();

			// 隐藏全屏点击收起按钮
			this.recoverButton.visibility=SlateVisibility.Collapsed
	}
}
/**唤醒确认结束对局对话框 */
const handleClick = (button: StaleButton) => {
	const buttonText = button.text; // 获取按钮上的文本
	UIService.show(MatchEndConfirmUI, buttonText); // 将按钮文本传递给 MatchEndConfirmUI
};
